﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;

namespace MonkeyWarranty.Web.Products
{
    public partial class Edit : System.Web.UI.Page
    {
        #region Properties

        private static ProductBiz _productBiz;
        private static ProductBiz ProductManager
        {
            get { return _productBiz ?? (_productBiz = new ProductBiz()); }
        }

        private int _productId;
        private int ProductId
        {
            get
            {
                if (_productId <= 0)
                    int.TryParse(Request.QueryString["Id"], out _productId);
                return _productId;
            }
        }

        private Product _product;
        private Product CurrentProduct
        {
            get
            {
                if (_product == null && ProductId > 0)
                {
                    _product = ProductManager.GetById(ProductId);
                }
                return _product;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Load product groups
                var groups = ProductManager.GetAllProductGroups();
                groups.Insert(0, new ProductGroup { Id = 0, Name = string.Empty });
                ddlProductGroups.DataTextField = "Name";
                ddlProductGroups.DataValueField = "Id";
                ddlProductGroups.DataSource = groups;
                ddlProductGroups.DataBind();

                // load manufacturers
                var manufacturers = ProductManager.GetAllManufacturers();
                manufacturers.Insert(0, new Manufacturer { Id = 0, Name = string.Empty });
                ddlManufacturers.DataTextField = "Name";
                ddlManufacturers.DataValueField = "Id";
                ddlManufacturers.DataSource = manufacturers;
                ddlManufacturers.DataBind();

                // load product
                if (CurrentProduct != null)
                {
                    ddlProductGroups.SelectedValue = CurrentProduct.ProductGroupId.ToString();
                    ddlManufacturers.SelectedValue = CurrentProduct.ManufacturerId.ToString();
                    txtCode.Text = CurrentProduct.Code;
                    txtName.Text = CurrentProduct.Name;
                    txtDescription.Text = CurrentProduct.Description;
                    txtSortOrder.Text = CurrentProduct.SortOrder.ToString(CultureInfo.InvariantCulture);
                    chkHidden.Checked = CurrentProduct.Hidden;
                    txtMeasurement.Text = CurrentProduct.Measurement;
                    chkWarranty.Checked = CurrentProduct.IsWarranty;
                    chkAccessary.Checked = CurrentProduct.IsAccessory;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                return;
            }
            var measurement = txtMeasurement.Text.Trim();
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;
            var isWarranty = chkWarranty.Checked;
            var isAccessory = chkAccessary.Checked;
            int productGroupId;
            int.TryParse(ddlProductGroups.SelectedValue, out productGroupId);
            int manufcturerId;
            int.TryParse(ddlManufacturers.SelectedValue, out manufcturerId);

            // get data
            CurrentProduct.Code = code;
            CurrentProduct.Name = name;
            CurrentProduct.Description = descrition;
            CurrentProduct.SortOrder = sortOrder;
            CurrentProduct.Hidden = isHidden;
            CurrentProduct.IsWarranty = isWarranty;
            CurrentProduct.IsAccessory = isAccessory;
            CurrentProduct.Measurement = measurement;
            CurrentProduct.ProductGroupId = productGroupId;
            CurrentProduct.ManufacturerId = manufcturerId;

            // save data
            ProductManager.UpdateProduct(CurrentProduct);

            // return
            Response.Redirect("List.aspx");
        }

        #endregion
    }
}